/*
 * DG (DG Gaming Engine)  - API
 * Copyright (C) 2005  Bob Marks (marksie531@yahoo.com)
 * http://code.google.com/p/dng-gaming
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package com.dg.gaming.client.gui;

import java.net.Socket;

import nanoxml.XMLElement;
import com.dg.gaming.client.ClientConnectionThread;
import com.dg.gaming.api.common.Game;
import com.dg.gaming.api.common.TransmissionException;
import com.dg.gaming.api.common.msg.*;
import com.dg.gaming.api.common.util.GameProperties;
import com.dg.gaming.api.common.util.DGLabels;

/**
 * Connection panel for games to connect.
 *
 * @author Bob
 * @version Beta 0.3
 */
public class GameConnectionPanel extends ConnectionPanel {

    /** Link the GUI. */
    private IGameGUI client;

    /** Link to the client connection thread for the game. */
    private ClientConnectionThread conn = null;

    /**
     * Constructor for an application which doesn't take a
     * username / password.
     *
     * @param client   Link to frame / applet.
     */
    public GameConnectionPanel(IGameGUI client) {
        super();

        this.client = client;
    }

    /**
     * Silent connect which takes a server, port, username and password.
     *
     * @param client    Link to frame / applet.
     * @param server    Name of the server.
     * @param port      Server port.
     * @param username  Username of person trying to connect.
     * @param password  Password of the person trying to connect.
     */
    public GameConnectionPanel(IGameGUI client,
                               String server,
                               int port,
                               long username,
                               String password) {
        super(server, port, username, password);

        this.client = client;
    }

    /**
     * Override the connect method for handling game traffic.
     *
     * @see com.dg.gaming.client.gui.awt.ConnectionPanel#connect(java.lang.String, int, java.lang.String, java.lang.String)
     */
    protected void connect(Socket socket,
                           long username,
                           String password) {
        // Create a new connection thread to handle communication
        this.conn = new ClientConnectionThread(socket, username, this);
        new Thread(conn).start(); // Start the thread

        // Create a connect message.
        MsgGameConnect commConnect;
        if (password.equals("")) {
            commConnect = new MsgGameConnect
                          (username, GameProperties.getGameID());
        } else {
            commConnect = new MsgGameConnect
                          (username, password, GameProperties.getGameID());
        }

        // Send connect message to the server.
        conn.send(commConnect);

    }

    /**
     * @see com.dg.gaming.client.gui.IClient#receiveGameMessage(nanoxml.XMLElement)
     */
    public void receiveGameMessage(XMLElement message) {
        String messageType = message.getName();

        // Message could be
        //     <error> - Error message.
        //     <game>  - Game message (success)
        if (messageType.equals(Msg.ERROR)) {
            // Create comm object and create error description
            MsgError commError = new MsgError(message);
            String errorStr = DGLabels.getError(commError.getStatus());

            // Update label
            statusLabel.setText(errorStr);
        } else if (messageType.equals(Msg.GAME)) {
            try {
                // Create game object on connection
                conn.setGame(new Game(message));

                // User is sucessfully connected so set game object and return object.
                client.connectOK(conn);
            } catch (TransmissionException tEx) {}
        }
    }

    public void receiveTableMessage(XMLElement message, String tableNum) {} // does nothing
}
